package vip.bblog.oauth2.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import vip.bblog.oauth2.entity.OauthClientDetails;
import vip.bblog.oauth2.service.OauthClientDetailsService;
import vip.bblog.common.utils.BaseResult;
import vip.bblog.common.utils.valid.Default;
import vip.bblog.common.utils.valid.Update;

/**
 * @author <a href="1396513066@qq.com">Yu Yong</a>
 * @version 1.0
 * @date 2019年05月10日 9:59
 * @desc ClientController 客户端配置
 */
@Api(tags = "Oauth2客户端控制器")
@RestController
@RequestMapping("/client")
public class OauthClientController {

    @Autowired
    private OauthClientDetailsService oauthClientDetailsService;


    @ApiOperation(value = "增加客户端", notes = "增加客户端")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientName", value = "客户端名称", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "scope", value = "授权范围", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "authorizedGrantTypes", value = "授权类型", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "webServerRedirectUri", value = "回调地址", required = true, dataType = "String", paramType = "query")
    })
    @PostMapping
    public BaseResult<Object> insert(@Validated(value = Default.class) @RequestBody OauthClientDetails clientDetails) {
        return oauthClientDetailsService.insert(clientDetails);
    }


    @ApiOperation(value = "删除客户端", notes = "根据clientId删除客户端")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID标识", required = true, dataType = "String", paramType = "delete"),
    })
    @DeleteMapping("/{clientId}")
    public BaseResult<Object> deleteById(@PathVariable(value = "clientId") String clientId) {
        return oauthClientDetailsService.deleteByClientId(clientId);
    }


    @ApiOperation(value = "更新客户端", notes = "根据clientId增加客户端")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID标识", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "clientName", value = "客户端名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "scope", value = "授权范围", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "authorizedGrantTypes", value = "授权类型", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "webServerRedirectUri", value = "回调地址", required = false, dataType = "String", paramType = "query")
    })
    @PutMapping
    public BaseResult<Object> updateById(@Validated(value = Update.class) @RequestBody OauthClientDetails clientDetails) {
        return oauthClientDetailsService.updateByClientId(clientDetails);
    }


    @ApiOperation(value = "模糊分页查询客户端", notes = "模糊查询客户端")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "clientId", value = "客户端ID标识", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "clientName", value = "客户端名称", required = false, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "状态", required = false, dataType = "int", paramType = "query"),
    })
    @GetMapping
    public BaseResult<Object> selectPageByExample(Page<OauthClientDetails> page, OauthClientDetails clientDetails) {
        return oauthClientDetailsService.selectPageByExample(page, clientDetails);
    }


    @ApiOperation(value = "clientId查询客户端", notes = "根据clientId查询客户端")
    @GetMapping("/{clientId}")
    public BaseResult<Object> select(@PathVariable(value = "clientId") String clientId) {
        return new BaseResult<>(oauthClientDetailsService.getById(clientId));
    }

}
